package com.winning.gjhlht.ihe.hl7ClientService.iheResponse;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.Element;

import com.cownew.ctk.common.RandomGUID;
import com.winning.common.Constans;
import com.winning.common.data.service.PlatBasicDataService;
import com.winning.common.tools.XMLTool;
import com.winning.core.base.IHEResponseServiceInter;
import com.winning.gjhlht.ihe.common.IHEConstans;

/**
 * 类名称：OrganizationRegistrationSeviceResponse
 * 类描述：  科室注册服务响应处理
 * 创建人：zq
 * 创建时间：2017-1-9 下午2:58:01
 * 修改人：zq
 * 修改时间：2017-1-9 下午2:58:01
 * 修改备注：
 */
public class OrganizationRegistrationSeviceResponse implements IHEResponseServiceInter {
    private static Logger LOGGER = Logger.getLogger(Constans.LOG4J_PBS_LOGGER_ID);

    //医疗机构信息信息注册返回结果xPath路径
    private static final String XPATH_TYPECODE_ATTR = "xsi:acknowledgement/@typeCode";

    //医疗机构注册返回结果说明xPath路径
    private static final String XPATH_TEXT_ELEM = "xsi:acknowledgement/xsi:acknowledgementDetail/xsi:text";

    //医疗机构注册返回国家平台标识号xPath路径/controlActProcess/subject/registrationRequest/subject1/registrationRequest/id
    private static final String XPATH_GJPTBSH_ATTR = "xsi:controlActProcess/xsi:subject/xsi:registrationRequest/xsi:subject1/xsi:assignedEntity/xsi:id/@extension";

    @Override
    public Map<String, Object> resonseHandler(PlatBasicDataService platBasicDataService, String responseXMLMessage, Map<String, Object> requestInfoMap, String dataSourseID) {


        Map<String, Object> registerResultInfo = new HashMap<String, Object>();

        //机构编码
        String jgbm = requestInfoMap.get("JGBM").toString();

        //机构名称
        String jgmc = requestInfoMap.get("JGMC").toString();

        //存放信息
        registerResultInfo.put("JGBM", jgbm);
        registerResultInfo.put("JGMC", jgmc);

        registerResultInfo.put("LSH", new RandomGUID().toString().replace("-", ""));


        registerResultInfo = readXMLRegisterResultHandler(registerResultInfo, responseXMLMessage);

        //存储注册信息
        int result = platBasicDataService.saveYLJGXXRegisterResultToDB(registerResultInfo, dataSourseID);

        if (result != -1) {
            LOGGER.info("--> result = [" + result + "] 医疗机构信息注册结果存入数据库成功 ");
        } else {
            LOGGER.info("--> result = [" + result + "] 医疗机构信息注册结果存入数据库失败 ");
        }

        return null;
    }


    /**
     * 解析注册机构，获取注册返回信息
     * 方法名称: readXMLRegisterResultHandler
     *
     * @param
     * @return Map<String   ,   Object>  返回类型说明<br/>
     * @throws <br/>
     * @author zhaqiang@winning.com.cn<br/>
     * 创建时间：2017-3-6/下午4:34:23<br/>
     */
    private Map<String, Object> readXMLRegisterResultHandler(Map<String, Object> prrb, String responseXMLMessage) {
        //个人注册返回结果文档
        Document document = null;

        try {
            //将返回结果转换成xml文档对象
            document = XMLTool.getDocumentFromXMLString(responseXMLMessage, true);

            if (document != null) {
                //根节点
                Element root = document.getRootElement();

                //获取注册返回结果
                String typeCode = XMLTool.selectAttributeValueByRootElement(root, XPATH_TYPECODE_ATTR);

                if (typeCode.equals(IHEConstans.JBXX_REGISTER_ZCJG_SUCCESS)) {
                    LOGGER.info("--> cxf返回结果处理  typeCode=[" + typeCode + "]  医疗机构信息注册成功 ");

                    //获取平台返回基本信息注册结果标识号
                    String gjptbsh = XMLTool.selectAttributeValueByRootElement(root, XPATH_GJPTBSH_ATTR);
                    prrb.put(IHEConstans.KEY_JBXX_REGISTER_GJPTBSH, gjptbsh);
                } else {
                    LOGGER.info("--> cxf返回结果处理  typeCode=[" + typeCode + "]  医疗机构信息注册失败 ");
                }

                //将注册结果信息存入map
                prrb.put(IHEConstans.KEY_JBXX_REGISTER_ZCJG, typeCode);

                //获取注册结果说明信息
                String zcjgsm = XMLTool.selectElementValueByRootElement(root, XPATH_TEXT_ELEM);
                prrb.put(IHEConstans.KEY_JBXX_REGISTER_ZCJGSM, zcjgsm);

                //获取注册日期
                //prrb.put(IHEConstans.KEY_JBXX_REGISTER_ZCRQ,DateConvert.getDateYYYY_MM_DD_HH_MM_SS(new Date()));
            }

        } catch (Exception e) {
            LOGGER.error(e, e.fillInStackTrace());
            e.printStackTrace();
        }
        return prrb;
    }
}
